package com.pure.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pure.common.Util;
import com.pure.dao.CollectMapper;
import com.pure.dao.ProductMapper;
import com.pure.dao.UserMapper;
import com.pure.db.Collect;
import com.pure.db.CollectExample;
import com.pure.db.Product;
import com.pure.db.ProductExample;
import com.pure.db.User;
import com.pure.service.MallService;
@Service
public class MallServiceImpl implements MallService {
	@Resource
	private ProductMapper productMapper;
	@Resource
	private CollectMapper collectMapper;
	@Resource
	private UserMapper userMapper;
	@Override
	public Integer addProduct(Product product) {
		return productMapper.insertSelective(product);
	}
	@Override
	public Integer updateProduct(Product product) {
		return productMapper.updateByPrimaryKeySelective(product);
	}
	@Override
	public Integer deleteProduct(Integer id) {
		return productMapper.deleteByPrimaryKey(id);
	}
	@Override
	public Integer addCollect(Collect collect) {
		return collectMapper.insertSelective(collect);
	}
	@Override
	public Integer cancelCollect(Integer id) {
		return collectMapper.deleteByPrimaryKey(id);
	}
	@Override
	public List<Product> getProductByUserId(Integer userId) {
		ProductExample example = new ProductExample();
		example.createCriteria().andUseridEqualTo(userId);
		return productMapper.selectByExample(example);
	}
	@Override
	public Product getProductById(Integer id) {
		return productMapper.selectByPrimaryKey(id);
	}
	@Override
	public Map<String, Object> getProductCollect(Integer productid) {
		CollectExample example = new CollectExample();
		example.createCriteria().andProductidEqualTo(productid);
		Integer count = collectMapper.countByExample(example);
		Map<String, Object> map = new HashMap<>();
		map.put("num", count);
		return map;
	}
	@Override
	public List<Product> searchProduct(String productname) {
		
		ProductExample example = new ProductExample();
		if (!Util.isNullString(productname)) {
			example.createCriteria().andProductnameLike("%"+productname+"%");
		}else {
			example.createCriteria();
		}
		
		return productMapper.selectByExample(example);
	}
	@Override
	public Integer insertBatch(List<Product> list) {
		return productMapper.insertBatch(list);
	}
	@Override
	public List<Map<String, Object>> searchProductAndSeller(String productname) {
		return productMapper.searchProductAndSeller(productname);
	}
	@Override
	public List<User> getProductCollectUserInfo(Integer productid) {
		Map<String, Object> map = new HashMap<>();
		map.put("productid", productid);
		return userMapper.getProductCollectUserInfo(map);
	}
	@Override
	public List<Map<String, Object>> getUsrCollect(Integer userid) {
		
		return productMapper.getUsrCollect(userid);
	}

}
